<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2018/1/16
 * Time: 10:33
 */

namespace Lib\Base;

use App;
use App\Http\Controllers\Base\Response;
use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Lib\Utils\ArrayUtils;
use Lib\Utils\ModelUtils;
use Exception;
class BaseController extends Controller
{
    use Response;
    protected $model;
    protected $foreign_key;
    protected $key_name = 'id';
    protected $type_name = 'type';
    protected $order_by = 'id';

    /**
     * BaseController constructor.
     */
    public function __construct()
    {
        $model_name = str_replace('Controller', '', get_class($this));

        $lastIndex = strripos($model_name, '\\');
        $model_name = substr($model_name, $lastIndex, strlen($model_name));
        try {
            $this->model = App::make('App\Http\Models' . $model_name);
            $this->key_name = $this->model->getKeyName();
        } catch (Exception $e) {

        }
    }
    //获取单个
    public function getOne(Request $request)
    {
        $query = $this->model->where($this->key_name, $request->input($this->key_name));
        return $this->data($this->getDatum($request,$query));
    }
    //获取多个
    public function get(Request $request, $query = null)
    {
        return $this->data($this->getData($request, $query));
    }

    public function getWithType(Request $request, $query = null)
    {
        $db_data = $this->getData($request, $query);
        $o = array();
        if ($db_data != null)
            foreach ($db_data as $item) {
                $o[$item[$this->type_name]][] = $item;
            }
        return $this->data($o);
    }

    public function getDatum(Request $request, $query = null)
    {
        $all = $request->all();
        if (sizeof($all) > 0) {
            foreach ($all as $key => $field) {
                if ($key == $this->key_name) continue;
                $query = $query->where($key, $field);
            }
        }
        $data = $query->firstOrFail();
        return $data;
    }

    public function getData(Request $request, $query)
    {
        $all = $request->all();
        foreach ($all as $key => $field) {
            if (!isset($query)) $query = $this->model->where($key, $field);
            else $query = $query->where($key, $field);
        }
        if (!isset($query)) $data = $this->model->orderByDesc($this->order_by)->get();
        else $data = $query->orderByDesc($this->order_by)->get();
        return $data;
    }

    public function edit(Request $request)
    {
        $park_id = $request->input($this->foreign_key);

        if (isset($park_id))
            $o = $this->model->where($this->key_name, $request->input($this->key_name))->where($this->foreign_key, $park_id)->first();
        else $o = $this->model->find($request->input($this->key_name));
        return $this->save($request, $o);
    }

    public function add(Request $request)
    {
        if ($this->model->create($request->all())) {
            return $this->message("添加成功");
        } else {
            return $this->error("添加失败");
        }
    }

    public function delete(Request $request)
    {
        $foreign_id = $request->input($this->foreign_key);
        $ids = $request->input($this->key_name);
        $count = 0;
        $model = $this->model;
        $ids = ArrayUtils::ToArray($ids);
        if (isset($ids))
            $query = $model->whereIn($this->key_name, $ids);
        if (isset($foreign_id))
            if (isset($query))
                $query = $query->where($this->foreign_key, $foreign_id);
        if (isset($query))
            $count = $query->delete();
        return $this->message($count);
    }

    public function store(Request $request)
    {
        $id = $request->input($this->key_name);
        return isset($id) ? $this->edit($request) : $this->add($request);

    }

    public function save(Request $request, $o)
    {
        if (isset($o)) {
            if (ModelUtils::requestToModel($request, $o) <= 0)
                return $this->error('必须添加修改字段');
            if ($o->save())
                return $this->message('修改成功');
        }
        return $this->error('修改失败');
    }


}